// wdl_weihouse/pages/user/brokeragent.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**已经选中的图片零时路径 */
    tempFilePaths: [],
    /**已经上传好的图片 url */
    form_image_paths: [],
    /**进度条 */
    show_progress: false,
    progress_per: 0,
    /**是否的登录 */
    islogin: false,
    /**经纪人数据 */
    broker:{},
  },
  /**获取是否已经有资料 */
  brokerinfo:function(){
    const self = this
    app.util.request({
      'url': 'entry/wxapp/brokeragent',
      data: { enews:'getinfo'},     
      success(res) {
          if(res.statusCode==200)   {
            if (!app.kbtools.isnull(res.data.data.result.avatar)){
            self.setData({ 
              broker: res.data.data.result,
              form_image_paths: res.data.data.result.avatar,
              tempFilePaths: [res.data.data.result.thumb]
             });  
            }
          }     
                  
      }
    });
  },
   
  /**上传图片函数 */
  uploadImages: function () {
    const self = this;
 
    let selected = self.data.tempFilePaths;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        /**显示选择的图片 */
        let tempFilePaths = res.tempFilePaths;         
        self.setData({
          tempFilePaths:  [tempFilePaths[0] ]
        });

        /**显示进度条 */
        self.setData({ show_progress: true, progress_per: 0 });
        /**调用接口上传 */
        let uploadUrl = app.util.url('entry/wxapp/transfile', { m: 'wdlebroker' });
        const uploadTask = wx.uploadFile({
          'url': uploadUrl,
          filePath: tempFilePaths[0],
          name: 'upfile',
          formData: {},
          success: function (res) {
            self.setData({ show_progress: false });
            let url = res.data;
            /**
             * res.data 的格式
             * "wdl_weihouse/201804/3275257180143965.jpg#https://api.guifun.com/attachment/wdl_weihouse/201804/3275257180143965.jpg"
             */
            let path = url.split('#');
            let form_image_paths = self.data.form_image_paths;
            self.setData({
              form_image_paths: path[0]
            });

          },
          fail: function (res) {
            console.log('失败返回数据', res)
          },
        })
        uploadTask.onProgressUpdate((res) => {
          self.setData({ progress_per: res.progress });
        })

      },
    })
  },
  /**提交保存表单 */
  formSubmit: function (event) {
    var self = this;
    var form = event.detail.value;
    //头头像信息
    form.avatar = self.data.form_image_paths;

    var field = ['nickname', 'company', 'mobile', 'vtags'];
    var ok = true;
    var params = {};
    for (var k in field) {
      var value = form[field[k]]
      if ( !value ) {
        wx.showToast({
          title: '请完善资料' ,
          icon: 'none',
          duration: 2000
        });
        ok = false && ok;
        break;
      }
    }
    if (!ok) {
      return false;
    }
    /**这个等级是经纪人1级 */
    form.groupid = 1;
    app.util.request({
      url: 'entry/wxapp/brokeragent',
      data: form,
      method: 'post',
      success: function (response) {
        wx.showToast({
          title: "保存成功！" ,
          icon: 'success',
          duration: 2000,
          success: function () {
            wx.switchTab ({
              url: '/wdlebroker/pages/user/index',
            })
          }
        });
      },
      fail: function (response) {
        if (response.data.message) {
          app.util.message(response.data.message, '', 'error');
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    /**判断登陆 强制登陆 */
    app.checkAuthorLogin(this);
    /**判断是否开通 */
    this.brokerinfo();
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})